using System.Collections.Generic;
using System.Web.Mvc;
using Moq;
using NUnit.Framework;
using RMH.Entities;
using RMH.Services;
using RMH.Web.Controllers;

namespace RMH.UnitTests.Web.Controllers.PatientControllerSpecs
{
    [TestFixture]
    public class PatientController_when_saving_a_patient : base_automock_test
    {
        private ActionResult result;
        List<County> counties = new List<County>();

        public override void establish_context()
        {
            Mock<ICountyService>()
                .Setup(s => s.GetAll())
                .Returns(counties);

        }
        public override void because()
        {
            result = Create<PatientController>().Edit(TestEntities.GetPatient());
        }

        [Test]
        public void the_patient_was_saved()
        {
            Mock<IPatientService>().
                Verify(s=>s.Save(TestEntities.GetPatient()));
        }
    }


    [TestFixture]
    public class PatientController_when_searching_patients : base_automock_test
    {
        private ViewResult result;
        IList<Patient> patients = new List<Patient>();


        // ARRANGE
        public override void establish_context()
        {
            patients.Add(new Patient { Id = 4, FirstName = "Joey", LastName = "Smith"});
            patients.Add(new Patient { Id = 7, FirstName = "Bob", LastName = "Dole" });
            Mock<IPatientService>()
                .Setup(ps => ps.Search(It.IsAny<string>(), It.IsAny<string>()))
                .Returns(patients);
        }

        // ACT
        public override void because()
        {
            result = Create<PatientController>().List("Joey","Smith");
        }

        // ASSERT
        [Test]
        public void gets_relevent_list_of_patients()
        {            
            (result.ViewData.Model as IList<Patient>).ShouldEqual(patients);
        }
    }


    [TestFixture]
    public class PatientController_when_searching_patients_brings_only_one_result : base_automock_test
    {
        private ViewResult result;
        IList<Patient> patients = new List<Patient>();

        // ARRANGE
        public override void establish_context()
        {
            patients.Add(new Patient{Id = 2});
            Mock<IPatientService>()
                .Setup(ps => ps.Search(It.IsAny<string>(), It.IsAny<string>()))
                .Returns(patients);
        }

        // ACT
        public override void because()
        {
            result = Create<PatientController>().List("Joey", "Smith");
        }

        // ASSERT
        [Test]
        public void returns_just_one_patient()
        {
            (result.ViewData.Model as IList<Patient>).ShouldBeNull();
            (result.ViewData.Model as Patient).ShouldEqual(patients[0]);
        }

        [Test]
        public void returns_detail_view()
        {
            result.ViewName.ShouldEqual("Details");
        }
    }    
}